$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllUser();
    disableBtn();
});

//遍历所有user输出到表格
function getAllUser() {
    let updateStr = "";
    let deleteStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "ID", data: "id"},
        {title: "用户名", data: "username"},
        {title: "邮箱", data: "email"},
        {title: "手机号", data: "tel"},
        {title: "电话", data: "phone"},
        {title: "所在单位ID", data: "departmentId"},
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                    if (hasDeletePermission) {
                        deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>"
                    }
                    if (hasUpdatePermission) {
                        updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateUserModal'>更新</button>";
                    }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = '/user/getAllUser';
    dataTableInit(url, columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/user/deleteUser');
    //为所有的update按钮绑定update事件
    updateBind('#toUpdateUser', '/user/updateUser', false, false, "#updateUserModal");
    //为所有的add按钮绑定add事件
    $('#toAddUser').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddUser')[0]);
        data.set('password', md5(data.get('password')));
        addAjax(data, '/user/addUser', false, false, "#addUserModal");
        return false;
    });
    //addBind( '#toAddUser', '/user/addUser', false, false, "#addUserModal");

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addUserModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddUser").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateUserModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateUser").reset();
    });
}

function disableBtn() {
    if (hasRoleSuperManger) {
        console.log("aaaa");
    }
}